package com.smile.frame.api.demo.controller;


import com.smile.frame.common.result.Result;
import com.smile.frame.config.log.systemlog.SystemLog;
import com.smile.frame.config.redis.service.RedisService;
import com.smile.frame.config.warn.bizWarn.BizWarn;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author LGC
 * @date 2021/8/27 9:52
 */
@Slf4j
@Api(tags = "demo")
@RestController
public class DemoController {
    //    @Resource
//    private RabbitUtil rabbitUtil;
    @Resource
    private RedisService redisService;


//    @SystemLog
//    @BizWarn(desc = "RABBIT_TEST 异常", dingTalk_handle = false)
//    @PostMapping(DemoUrl.TEST)
//    @ApiOperation(value = "TEST")
//    public ResponseVO<Demo> test(@Validated @RequestBody DemoDTO dto) throws InterruptedException {
//        Demo demo = CopyUtil.copy(dto, Demo.class);
//// 异常测试
////        if (StrUtil.isBlank(demo.getPhone())) {
////            throw new BizException(DemoErrorCodeEnum.DEMO_TEST_ERROR);
////        }
//
//// 重复提交测试
//        Thread.sleep(5000);
//
//// rabbitmq 测试
////        log.info("同一个队列处理不同结构message");
//        rabbitUtil.sendMessage(RabbitBindEnum.DEMO_BIND, MqMessageEnum.DEMO_ONE_MESSAGE, demo);
////        rabbitUtil.sendMessage(RabbitBindEnum.DEMO_BIND, MqMessageEnum.DEMO_TWO_MESSAGE, demo);
//
//        return ResponseVO.ok(demo);
//    }

//    @PostMapping(DemoUrl.REDIS_KEY_EXPIRE_TEST)
//    @ApiOperation(value = "redisKeyExpireTest")
//    @Transactional(rollbackFor = Exception.class)
//    public ResponseVO<Void> redisKeyExpireTest(@Validated @RequestBody DemoReservationInfoDO dto) {
//        Date expire_date = dto.getExpire_date();
//        Date now_date = new Date();
//        if (now_date.before(expire_date)) {
//            DemoReservationInfoDO demoReservationInfoDO = null;
//            for (int i = 0; i < 20000; i++) {
//                demoReservationInfoDO = CopyUtil.copy(dto, DemoReservationInfoDO.class);
//                demoReservationInfoDOMapper.insert(demoReservationInfoDO);
//                long between = DateUtil.between(expire_date, now_date, DateUnit.SECOND);
//                log.info("未来时间,设置key过期监听,{}秒后到期", between);
//                redisService.set("reservation:" + demoReservationInfoDO.getId(), demoReservationInfoDO.getId(), between);
//            }
//        }
//        return ResponseVO.ok();
//    }


//    @PostMapping(DemoUrl.RABBIT_KEY_EXPIRE_TEST)
//    @ApiOperation(value = "rabbitKeyExpireTest")
//    @Transactional(rollbackFor = Exception.class)
//    public ResponseVO<Void> rabbitKeyExpireTest(@Validated @RequestBody DemoReservationInfoDO dto) {
//        Date expire_date = dto.getExpire_date();
//        Date now_date = new Date();
//        if (now_date.before(expire_date)) {
//            demoReservationInfoDOMapper.insert(dto);
//            long between = DateUtil.between(expire_date, now_date, DateUnit.SECOND);
//            log.info("未来时间,发送延时队列,{}秒后到期", between);
//            rabbitUtil.sendDelayMessage(RabbitBindEnum.DELAY_BIND, MqMessageEnum.RESERVATION_CLOSE_MESSAGE, dto, Math.toIntExact(between));
////            ReservationInfo reservationInfo = null;
////            for (int i = 0; i < 20000; i++) {
////                reservationInfo = CopyUtil.copy(dto, ReservationInfo.class);
////                reservationInfoMapper.insert(reservationInfo);
////                long between = DateUtil.between(expire_date, now_date, DateUnit.SECOND);
////                log.info("未来时间,设置key过期监听,{}秒后到期", between);
////                redisService.set("reservation:" + reservationInfo.getId(), reservationInfo.getId(), between);
////            }
//        }
//        return ResponseVO.ok();
//    }

    @SystemLog
    @BizWarn(desc = "导出 异常")
    @PostMapping("/export")
    @ApiOperation(value = "导出")
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        QueryDistrictReservationOrderPageListDTO dto = LocalHttpUtil.getRequestParam(request, QueryDistrictReservationOrderPageListDTO.class);
//        List<Map<String, Object>> list = new ArrayList<>();
//        for (DistrictReservationOrderListVO record : records) {
//            Map<String, Object> map = HuToolExcelUtil.entityToMap(record, null);
//            list.add(map);
//        }
//        HuToolExcelUtil.commonExcelExport(response, list, reservationDistrictDO.getDistrict_name() + DateUtil.format(new Date(), "yyyyMMddHHmm") + "订单");

    }


    @SystemLog
    @BizWarn(desc = "导入 异常")
    @PostMapping(value = "/importFile")
    @ApiOperation(value = "导入")
    public Result<Void> importFile(@ApiParam(name = "导入文件", required = true) MultipartFile file) throws IOException {
//        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
//        reader.addHeaderAlias("名称", "agent_name");
//        reader.addHeaderAlias("手机号", "agent_phone");
//        List<ReservationAgentDO> agentDOS = reader.read(1, 2, ReservationAgentDO.class);
        return Result.ok();
    }


    @GetMapping("/addStock/{num}")
    @ApiOperation("addStock")
    public Result<Integer> redissonLock(@PathVariable("num") Integer num) throws Exception {


        // 日活数量
        for (int i = 0; i < 1000; i++) {
            redisService.bitSet("online:2021-11-10", i, true);
        }


        Long aLong = redisService.bitCount("online:2021-11-10");
        log.info("2021-11-10日活数量：{}", aLong);

        // 文章点赞
        for (int i = 0; i < 1000; i++) {
            Boolean aBoolean = redisService.bitHasKey("like:note_1", i);
            if (aBoolean) {
                log.info("用户1 已点赞,取消点赞");
                redisService.bitSet("like:note_1", i, false);
            } else {
                redisService.bitSet("like:note_1", i, true);
            }
        }
        log.info("note_1文章点赞数量:{}", redisService.bitCount("like:note_1"));


        return Result.ok();

    }


    @GetMapping("/setClusterRedisKey")
    @ApiOperation("setClusterRedisKey")
    public Result<Integer> setClusterRedisKey() throws Exception {
        for (int i = 1; i <= 1000; i++) {
            redisService.set("keys2:key" + i, i, 600);
//            log.info("{}",redisService.set("keys:mykey" + i,i,600););
        }
        return Result.ok();
    }


    public static double[] calculate(int r) {
        // write your code here
        double pi = 3.14d;
        double[] a = new double[2];
        a[0] = 2 * pi * r;
        a[1] = pi * Math.pow(r, 2);
        return a;
    }

    public static List<String> printX(int n) {
        ArrayList<String> res = new ArrayList<>();//创建一个存放String类型属性的ArrayList，名称为res
        char[] line = new char[n];//创建一个长度为n的字符型数组line
        for (int i = 0; i < n; i++) {//外循环遍历正整数n的次数
            for (int j = 0; j < n; j++) {//该循环为了辅助初始化字符型数组中的每个属性
                line[j] = ' ';//初始化字符型数组中的每个属性
            }
            line[i] = 'X';//左边对应位置赋值"X“
            line[n - i - 1] = 'X';//右边对应位置赋值“X”
            res.add(String.valueOf(line));//将得到的line转换成字符串的形式增加到ArrayList
        }
        return res;//返回ArrayList
    }

    public static List<Integer> prime(int n) {
        // write your code here
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 2; i <= n; i++) {
            if (n == 2 || n % 2 == 1) {
                int a = 0;
                for (int j = 2; j < i; j++) {
                    if (i % j == 0) {// 是否是因数
                        a++;
                    }
                }
                if (a == 0) {
                    list.add(i);
                }
            }
        }
        return list;
    }

    public static void main(String[] args) {
        System.out.println(prime(10));
    }

}
